package com.wrupple.muba.catalogs.client.widgets.impl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.module.services.logic.MultipartFormActionUrlService;

public class CatalogFileUpload extends Composite implements HasValue<String> {

	private static CatalogFileUploadUiBinder uiBinder = GWT
			.create(CatalogFileUploadUiBinder.class);

	interface CatalogFileUploadUiBinder extends
			UiBinder<Widget, CatalogFileUpload> {
	}

	private class CompleteHandler implements SubmitCompleteHandler {

		@Override
		public void onSubmitComplete(SubmitCompleteEvent event) {
			// Response an be either a JSON download descriptor, or a plain
			// string with the id of the file
			String newFile = event.getResults();
			JsArrayString array = calculateNewValue(newFile);
			String newValue = null;
			if (array.length() == 0) {
				newValue = null;
			} else {
				newValue = array.get(0);
			}
			value = newValue;
			setValue(newValue, true);
			submit.setEnabled(false);
			headerText.setText("Upload Success!");
		}

	}


	protected String value;
	@UiField
	FormPanel form;
	@UiField
	Button submit;
	@UiField InlineLabel headerText;
	@UiField
	FileUpload uploader;
//vegetate/catalog/file/url

	@Inject
	public CatalogFileUpload(MultipartFormActionUrlService actionUrl) {
		this(actionUrl, false);
	}

	public CatalogFileUpload(MultipartFormActionUrlService actionUrl, boolean multiple) {
		initWidget(uiBinder.createAndBindUi(this));
		actionUrl.setUploadUrl(form);

		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		form.addSubmitCompleteHandler(new CompleteHandler());
		uploader.setName("file");
	}

	public void setAction(String action) {
		form.setAction(action);
	}

	public String getFileName() {
		return uploader.getFilename();
	}

	protected native JsArrayString calculateNewValue(String newFile) /*-{
																		return eval(newFile);
																		}-*/;

	@Override
	public HandlerRegistration addValueChangeHandler(
			ValueChangeHandler<String> handler) {
		return addHandler(handler, ValueChangeEvent.getType());
	}

	@Override
	public String getValue() {
		return value;
	}

	@Override
	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public void setValue(String value, boolean fireEvents) {
		setValue(value);
		if (fireEvents) {
			ValueChangeEvent.fire(this, getValue());
		}
	}

	@UiHandler("submit")
	public void submit(ClickEvent ev) {
		form.submit();
	}

	

}
